package cn.xiaomubiao.oschine.fragment.Event_fragment;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.xiaomubiao.oschine.MyApplication;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.adapter.EventAdapter;
import cn.xiaomubiao.oschine.base.BaseEachFragment;
import cn.xiaomubiao.oschine.base.BaseRecyclerViewAdapter;
import cn.xiaomubiao.oschine.bean.Event;
import cn.xiaomubiao.oschine.bean.EventList;
import cn.xiaomubiao.oschine.contants.Host;
import cn.xiaomubiao.oschine.util.HttpUtil;
import cn.xiaomubiao.oschine.util.ItemDivider;

/**
 * Created by King Su on 2017/4/7.
 */

public class EventBaseFragment2 extends BaseEachFragment {


   @BindView(R.id.rv_event_pager)
    XRecyclerView mRvEventPager;
    @BindView(R.id.event_pager)
    LinearLayout mEventPager;
    Unbinder unbinder;
    private View mView;



    private EventList mEventList;
    private List<Event> mEvent;
    private int loadingNum;
    private EventAdapter mEventXRecylerAadapter;

    @Override
    public View loadSuccess() {
        mView = View.inflate(getActivity(), R.layout.view_event_pager_layout, null);
        unbinder = ButterKnife.bind(this, mView);
        initRecylerView();
        return mView;
    }

  /**
     * 初始化RecylerView
     */
    private void initRecylerView() {
        mEventXRecylerAadapter = new EventAdapter(mEvent);
        mRvEventPager.addItemDecoration(new ItemDivider().setDividerColor(Color.parseColor("#AEAEAE")));
        mRvEventPager.setNoMore(true);
        mEventXRecylerAadapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnRecycleViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                    String url =  "Host.HOST_DEFAULT"+"list/my_event_list/page0.xml";
                    Toast.makeText(getContext(),url, Toast.LENGTH_SHORT).show();
                    //String say = "ttp://192.168.1.100:8080/oschina/commentlist/catalog3id"+mTweets.get(position).getId()+"page"+loadingNum+".xml";
               Intent intent = new Intent(getActivity(), EventInfoBaseActivity.class);
                //intent.putExtra("id",mEvent.get(position).getId());
                intent.putExtra("id",243094);
                intent.putExtra("title","活动详情");
                intent.putExtra("eventTitle","【J+】互联网沙龙——大数据安全与行业实战分享");
                intent.putExtra("time","2015-07-26 14:00:00/n2015-07-26 17:30:00");
                intent.putExtra("address","济南"+"     "+"高新区天泺路88号展威科技园1号楼A座7层 易构软件");
                intent.putExtra("className",EventFragment.class.getName());
                startActivity(intent);
                }
        });
        mRvEventPager.setAdapter(mEventXRecylerAadapter);
        mRvEventPager.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvEventPager.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
               // Toast.makeText(getContext(), "刷新", Toast.LENGTH_SHORT).show();
                MyApplication.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRvEventPager.refreshComplete();
                    }
                },2000);
            }

            @Override
            public void onLoadMore() {
                MyApplication.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRvEventPager.loadMoreComplete();
                        mRvEventPager.setNoMore(true);
                    }
                },2000);
            }
        });
    }


    @Override
    public void loadData() {
        HttpUtil<EventList> httpUtil = new HttpUtil<>(getContext(),EventList.class);
        String url = Host.HOST_DEFAULT+"list/my_event_list/page0.xml";
        httpUtil.getStringData(url, new HttpUtil.OnGetDataListener<EventList>() {
            @Override
            public void onSuccess(EventList eventList) {
                mEvent = eventList.getList();
                mCommonPager.showSuccessPager(mEvent);
            }

            @Override
            public void onFailure(Exception e) {
                mCommonPager.showNoLogin();
            }
        });

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
